// server/app/controller/admin/job_opening.js
'use strict';

const BaseController = require('../../core/base_controller');

class JobOpeningController extends BaseController {
  // 获取岗位列表
  async index() {
    const { ctx } = this;
    const { page = 1, pageSize = 10, title, status, position_id } = ctx.query;
    
    const where = { del_flag: 0 };
    if (position_id) where.position_id = position_id;
    if (status !== undefined) where.status = parseInt(status);

    const { count, rows } = await ctx.model.JobOpening.findAndCountAll({
      where,
      order: [['created_at', 'DESC']],
      offset: (page - 1) * pageSize,
      limit: parseInt(pageSize),
      include: [
        { model: ctx.model.ChefPosition, as: 'position' },
        { model: ctx.model.DictionaryDetail, as: 'cuisine' },
        { model: ctx.model.DictionaryDetail, as: 'workExperience' },
        { model: ctx.model.DictionaryDetail, as: 'jobType' },
        { model: ctx.model.User, as: 'publisher', attributes: ['user_id', 'phone'] }
      ]
    });

    this.success({
      list: rows,
      pagination: {
        total: count,
        current: parseInt(page),
        pageSize: parseInt(pageSize),
      },
    });
  }

  // 创建岗位
  async create() {
    const { ctx } = this;
    
    const rules = {
      position_id: { type: 'number', required: true, message: '请选择岗位' },
      work_place: { type: 'string', required: true, message: '请输入工作地点' },
      min_salary: { type: 'number', required: true, message: '请输入最低薪资' },
      max_salary: { type: 'number', required: true, message: '请输入最高薪资' },
      description: { type: 'string', required: true, message: '请输入岗位职责' }
    };

    try {
      ctx.validate(rules);
    } catch (err) {
      ctx.throw(400, err.errors);
    }

    const data = {
      ...ctx.request.body,
      user_id: ctx.state.user.user_id,
      public_time: new Date()
    };

    const job = await ctx.model.JobOpening.create(data);
    this.success(job);
  }

  // 更新岗位
  async update() {
    const { ctx } = this;
    const id = ctx.params.id;
    
    const job = await ctx.model.JobOpening.findByPk(id);
    if (!job) {
      ctx.throw(404, '岗位不存在');
    }

    const rules = {
      position_id: { type: 'number', required: false },
      work_place: { type: 'string', required: false },
      min_salary: { type: 'number', required: false },
      max_salary: { type: 'number', required: false },
      description: { type: 'string', required: false }
    };

    try {
      ctx.validate(rules);
    } catch (err) {
      ctx.throw(400, err.errors);
    }

    await job.update(ctx.request.body);
    this.success(job);
  }

  // 删除岗位
  async destroy() {
    const { ctx } = this;
    const id = ctx.params.id;

    const job = await ctx.model.JobOpening.findByPk(id);
    if (!job) {
      ctx.throw(404, '岗位不存在');
    }

    await job.update({ del_flag: 1 });
    this.success(null, '删除成功');
  }

  // 更新状态
  async updateStatus() {
    const { ctx } = this;
    const id = ctx.params.id;
    
    const rules = {
      status: { type: 'number', required: true, enum: [0, 1], message: '状态值无效' }
    };

    try {
      ctx.validate(rules);
    } catch (err) {
      ctx.throw(400, err.errors);
    }

    const job = await ctx.model.JobOpening.findByPk(id);
    if (!job) {
      ctx.throw(404, '岗位不存在');
    }

    await job.update({ status: ctx.request.body.status });
    this.success(null, '状态更新成功');
  }
}

module.exports = JobOpeningController;